<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/10/9
 * Time: 11:48
 */

namespace App\Models\user\observe;


use App\Jobs\user\BuildUserSuperior;
use App\Models\config\Config;
use App\Models\User;
use App\Models\user\Depart;
use App\Models\user\UserSuperior;
use App\Services\common\App;
use phpDocumentor\Reflection\Types\Null_;

class UserObserve
{
    public function creating(User $user)
    {
        $id   = User::orderBy('id','desc')->value('id');
        
//        $user->uuid = App::getUuid($id);

        if (empty($user->name)){
            $user->name =  config('app.name').'用户'.substr($user->phone,-4);
        }

        if (empty($user->sex)){
            $user->sex = 1;
        }
        $user->sex = 1;
        if (empty($user->header_img)){
            $user->header_img =  Config::getConfig('default_header');
        }

        if (empty($user->money)){
            $user->money = 0;
        }

        if (empty($user->level)){
            $user->level = 1;
        }

        if($user->level == 3){
            $level3 = User::where('level',3)->where('id','!=',$user->id)->first();
            if($level3){
                $user->level = 1;
            }
        }
        if($user->level == 2){
            $depart_id_arr = explode(',',$user->depart_id);
            foreach ($depart_id_arr as $item){
//                $depart_leader = User::where('level',2)
//                    ->where('depart_id',$item)->first();
                $depart_leader = Depart::where('id',$item)->first();
                if($depart_leader && $depart_leader['leader_id'] != $user->id){
                    User::where('id',$depart_leader['id'])->update(['level' => 1]);
                }
            }
        }
        if($user->level == 1){
//            $depart_leader = User::where('level',2)->where('depart_id',$user['depart_id'])->first();
            $depart_leader = Depart::where('id',$user['depart_id'])->first();
            if(!$depart_leader['leader_id']){
                $user->level = 2;
            }
        }

    }

    public function saving(User $user)
    {
        if($user->level == 3){
            $level3 = User::where('level',3)->where('id','!=',$user->id)->first();
            if($level3){
                $user->level = 1;
            }
        }
        if($user->level == 2){
            $depart_id_arr = explode(',',$user->depart_id);
            foreach ($depart_id_arr as $item){
//                $depart_leader = User::where('level',2)
//                    ->where('depart_id',$item)->first();
                $depart_leader = Depart::where('id',$item)->first();
                if($depart_leader && $depart_leader['leader_id'] != $user->id){
                    User::where('id',$depart_leader['id'])->update(['level' => 1]);
                }
            }
        }
        if($user->level == 1){
//            $depart_leader = User::where('level',2)->where('depart_id',$user['depart_id'])->first();
            $depart_leader = Depart::where('id',$user['depart_id'])->first();
            if(!$depart_leader['leader_id']){
                $user->level = 2;
            }
        }
    }

    public function saved(User $user)
    {
        if($user->level == 2){
            $depart_id_arr = explode(',',$user->depart_id);
            Depart::where('leader_id',$user->id)->update(['leader_id'=>null]);
            foreach ($depart_id_arr as $item){
                $depart_leader = User::where('level',2)
                    ->where('depart_id',$item)->first();
                if($depart_leader && $depart_leader['id'] != $user->id){
                    User::where('id',$depart_leader['id'])->update(['level' => 1]);
                }
                Depart::where('id',$item)->update(['leader_id'=>$user->id]);
            }
        }
    }

    public function created(User $user)
    {

    }


    public function updated(User $user)
    {

//        if ($user->status == 0){
//
//        }

//        if($user->level != $user->getOriginal('level')){
//            $update['user_level'] = $user->level;
//            if ($user->level == 5){
//                $update['is_top'] = 1;
//            }else{
//                $update['is_top'] = 0;
//            }
//            UserSuperior::where(['superior_id'=>$user->id])->update($update);
//        }

    }

    public function deleted(User $user)
    {



    }

    public function updating(User $user)
    {
//        var_dump($user);exit;
    }
}